#include <stdio.h>

// Declare uninstatiated global variables 
// that can be modified throughout the program
int iAverage, iVariance;

// Declare and initialise global const variable
// This can be accessed throughout the entire
// program, but not modified post initialization
const int iArr[5] = {1, 3, 5, 7, 9};

int calcAvg(const int *iArr);
int calcVar(const int *iArr, int iMean);

int main(int argc, char ** argv)
{
	iAverage = calcAvg(iArr);
	iVariance = calcVar(iArr, iAverage);

	printf("Average is: %d \n", iAverage);
	printf("Variance is: %d \n", iVariance);
    	return 0;
}

/*
Function to calculate average, takes an integer
array as it's sole parameter
*/
int calcAvg(const int *iArr)
{
	int iResult = 0;
	int i = 0;
	for(i=0; i<5; i++)
	{
		iResult += iArr[i];
	}

	//Primitive error handling to guard against divide by zero
	if(iResult % 5 > 0)
	{
		return -1;
	}
	else
	{
		return iResult/5;
	}
}

/*
Function to calculate variance. Variance can defined simply as:
"The average of the squared differences from the Mean".
This function takes the const array of integers as one of
its parameters, and the interger result of the calcAvg function
as it's second parameter
*/
int calcVar(const int *iArr, int iMean)
{
	int iTemp = 0;
	int i = 0;
	for(i=0; i<5; i++)
        {       
		iTemp = iArr[i] - iMean;
		iTemp = iTemp * iTemp;
               	iVariance += iTemp;
        }

	//Primitive error handling to guard against divide by zero
        if(iVariance % 5 > 0)
        {       
                return -1;
        }
	else
	{
		return iVariance/5;
	}
}
